<template>
  <div>
    <div class="table-page-search">
      <a-form>
        <a-form-item label="名称">
          <a-input placeholder="请输入" v-model="queryParam.keyWord" />
        </a-form-item>
        <a-form-item label="状态">
          <a-select placeholder="请选择" v-model="queryParam.status">
            <a-select-option :value="''">全部</a-select-option>
            <a-select-option :value="1">正常</a-select-option>
            <a-select-option :value="0">禁用</a-select-option>
          </a-select>
        </a-form-item>
        <span class="search-submitButtons">
          <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
          <a-button @click="() => (queryParam = { status: '' })">重置</a-button>
        </span>
      </a-form>
      <div class="table-operator disFlxJS">
        <div>
          <!-- <a-button type="primary" @click="handleAdd">新增</a-button> -->
          <a-button type="primary" @click="handleAddCategory">添加类别</a-button>
        </div>
        <CustomTable :name="$route.name + '_' + 'Organization'" :columns="columns" :newColumns="newColumns" />
      </div>
    </div>

    <s-table size="default" ref="table" rowKey="userId" :rowSelection="null" :columns="newColumns" :data="loadData">
      <span slot="serial" slot-scope="text, record, index">
        {{ (pageNum - 1) * pageSize + index + 1 }}
      </span>
      <span slot="status" slot-scope="text, record">
        <a-switch :checked="record.enabled === 1" @change="onChangeStatus(record)" />
      </span>
      <span slot="action" slot-scope="text, record">
        <a-button type="primary" ghost size="small" class="_mr-5" @click="handleEdit(record)"> 编辑 </a-button>
        <a-button type="primary" ghost size="small" class="_mr-5" @click="delByIds([record.id])"> 删除 </a-button>
      </span>
    </s-table>
    <data-form ref="dataForm" @add="$refs.table.refresh(true)" @upd="$refs.table.refresh()" />
    <treeTransfer ref="treeTransferRef" :api="getOrganizationCategoryTree" title="单位类别" @success="handleSuccess"></treeTransfer>
  </div>
</template>

<script>
/**单位类别 */
import { STable } from '@/components'
import { memberTypeGetList, memberTypeEnable, memberTypeDel } from '@/api/system/system'
import dataForm from './modules/dataForm'
import CustomTable from '@/components/Cuscolumn'
import treeTransfer from '../../../components/treeTransfer/index'
import { getOrganizationCategoryTree, addOrganizationCategory } from '@/api/member/memberConfig'
export default {
  name: 'MemberType',
  components: {
    STable,
    dataForm,
    CustomTable,
    treeTransfer
  },
  data() {
    return {
      // 查询参数
      queryParam: { status: '' },
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' },
          width: '80px'
        },
        {
          title: '名称',
          dataIndex: 'name',
          width: '200px',
          ellipsis: true
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: '200px',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '排序',
          dataIndex: 'sort',
          width: '80px',
          ellipsis: true
        },
        {
          title: '备注',
          dataIndex: 'remark',
          width: '200px',
          ellipsis: true
        },
        {
          title: '操作',
          width: '150px',
          fixed: 'right',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      pageNum: 1,
      pageSize: 10,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return memberTypeGetList(Object.assign(parameter, this.queryParam)).then(res => {
          this.pageNum = parameter.pageNum
          this.pageSize = parameter.pageSize
          return res
        })
      }
    }
  },
  computed: {
    newColumns() {
      const currentColumns = this.$store.state.user.customTable
      if (currentColumns && currentColumns[this.$route.name + '_' + 'Organization']) {
        return this.$options
          .data()
          .columns.filter(v => {
            const _items = currentColumns[this.$route.name + '_' + 'Organization'].find(
              item => item === v.title || item.title === v.title
            )
            if (_items) return Object.assign(v, _items)
          })
          .sort((a, b) => {
            return a.tableSort - b.tableSort
          })
      } else {
        return this.columns
      }
    }
  },
  created() {},
  methods: {
    getOrganizationCategoryTree,
    handleAdd() {
      var params = {
        title: '新增单位类别',
        type: 'add'
      }
      this.$refs.dataForm.add(params)
    },
    handleEdit(record) {
      var params = {
        title: '编辑单位类别',
        type: 'edit',
        data: record
      }
      this.$refs.dataForm.edit(params)
    },
    handleAddCategory() {
      this.$refs.treeTransferRef.openModal()
    },
    async handleSuccess(checkedKeys) {
      await addOrganizationCategory(checkedKeys)
      this.$message.success(`添加成功`)
      this.$refs.treeTransferRef.closeModal()
      this.handleReload()
    },
    handleReload() {
      this.$refs.table.refresh()
    },
    delByIds(ids) {
      var _this = this
      this.$confirm({
        title: '删除?',
        content: '是否确定删除该组织类别?',
        onOk() {
          memberTypeDel({ ids: ids.join(',') }).then(res => {
            if (res.code === 0) {
              _this.$message.success(`删除成功`)
              _this.handleReload()
            } else {
              _this.$message.error(res.msg)
            }
          })
        },
        onCancel() {}
      })
    },

    onChangeStatus(record) {
      var msg = ''
      if (record.enabled === 0) {
        msg = '启用成功'
      } else {
        msg = '禁用成功'
      }
      record.status = record.enabled === 0 ? 1 : 0
      memberTypeEnable(record).then(res => {
        if (res.code === 0) {
          this.$message.success(msg, 1)
          this.$refs.table.refresh()
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  }
}
</script>
